using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;


using gudusoft.gsqlparser;
using gudusoft.gsqlparser.Units;

namespace formatsql
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void setupformatoptions()
        {
            //for more format options, please check document

            lzbasetype.gFmtOpt.Select_Columnlist_Style = TAlignStyle.asStacked;
            lzbasetype.gFmtOpt.Select_Columnlist_Comma = TLinefeedsCommaOption.lfAfterComma;
            lzbasetype.gFmtOpt.SelectItemInNewLine = false;
            lzbasetype.gFmtOpt.AlignAliasInSelectList = true;
            lzbasetype.gFmtOpt.TreatDistinctAsVirtualColumn = false;

            //setup more format options ...
            
            lzbasetype.gFmtOpt.linenumber_enabled = false;
        }

        private void setuphighlighterAttributes()
        {
            lzbasetype.gFmtOpt.HighlightingFontname = "Courier New";
            lzbasetype.gFmtOpt.HighlightingFontsize = 10;

            //for other elements you want to customize, please check document
            lzbasetype.gFmtOpt.HighlightingElements[(int)TLzHighlightingElement.sfkIdentifer].SetForegroundInRGB("#008000");
            lzbasetype.gFmtOpt.HighlightingElements[(int)TLzHighlightingElement.sfkIdentifer].StyleBold = true;
            lzbasetype.gFmtOpt.HighlightingElements[(int)TLzHighlightingElement.sfkIdentifer].StyleItalic = false;
            lzbasetype.gFmtOpt.HighlightingElements[(int)TLzHighlightingElement.sfkIdentifer].StyleStrikeout = false;
            lzbasetype.gFmtOpt.HighlightingElements[(int)TLzHighlightingElement.sfkIdentifer].StyleUnderline = false;

            //setup more elements attributes ....
        }


        private void btnFormatSQL_Click(object sender, EventArgs e)
        {
            tabControl1.SelectedIndex = 0;
            setupformatoptions();
            outputsql.Clear();

            //lzbasetype.gFmtOpt.InitDefaultConfig();

            TGSqlParser sqlparser = new TGSqlParser(getDBVendor());
            
            sqlparser.SqlText.Text = inputsql.Text;
            int i = sqlparser.PrettyPrint();
            if (i == 0)
            {
                outputsql.Text = sqlparser.FormattedSqlText.Text;
                //for (int j = 0; j < sqlparser.SourceTokenList.Count(); j++)
                //{
                //    TSourceToken st = sqlparser.SourceTokenList[j];
                //    outputsql.Text = outputsql.Text + Environment.NewLine + st.AsText + " ,code:" + st.TokenCode + " ,type:" + st.TokenType;
                //}
            }
            else
            {
                outputsql.Text = sqlparser.ErrorMessages;
                for (int j = 0; j < sqlparser.SourceTokenList.Count() ; j++)
                {
                    TSourceToken st = sqlparser.SourceTokenList[j];
                    outputsql.Text = outputsql.Text +Environment.NewLine+ st.AsText+" ,code:"+st.TokenCode+" ,type:"+st.TokenType;
                }
            }
        }

        private void btnFormatSqlToHtml_Click(object sender, EventArgs e)
        {
            tabControl1.SelectedIndex = 1;
            setupformatoptions();
            setuphighlighterAttributes();

            TGSqlParser sqlparser = new TGSqlParser(getDBVendor());
            sqlparser.SqlText.Text = inputsql.Text;
            webBrowser1.DocumentText = sqlparser.ToHtml(TOutputFmt.ofhtml);

        }

        private void btnFormatSQLInRTF_Click(object sender, EventArgs e)
        {
            tabControl1.SelectedIndex = 2;
            setupformatoptions();
            setuphighlighterAttributes();

            TGSqlParser sqlparser = new TGSqlParser(getDBVendor());
            sqlparser.SqlText.Text = inputsql.Text;
           // richTextBox1.Text = sqlparser.ToRTF(TOutputFmt.ofrtf);
            richTextBox1.Rtf = sqlparser.ToRTF(TOutputFmt.ofrtf);

        }

        private void button1_Click(object sender, EventArgs e)
        {
            Process ps = new Process();
            string yourURL = "http://www.sqlparser.com/buynow.php?ref=csharp_format_demo";
            ps.StartInfo.FileName = "iexplore.exe";
            ps.StartInfo.Arguments = yourURL;
            ps.Start();  

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            
            cmbdatabases.SelectedIndex = 0;
            
        }

        private TDbVendor getDBVendor()
        {
            TDbVendor dbVendor = TDbVendor.DbVMssql;
            switch (cmbdatabases.SelectedIndex)
            {
                case 0: 
                    dbVendor = TDbVendor.DbVMssql;
                    break;
                case 1:
                    dbVendor = TDbVendor.DbVOracle;
                    break;
                case 2:
                    dbVendor = TDbVendor.DbVDB2;
                    break;
                case 3:
                    dbVendor = TDbVendor.DbVMysql;
                    break;
                case 4:
                    dbVendor = TDbVendor.DbVAccess;
                    break;
                case 5:
                    dbVendor = TDbVendor.DbVMdx;
                    break;
                default:
                    break;
            }

            return dbVendor;
        }
       
    }
}